const xlsx = require('node-xlsx'),
request = require('request'),
fs = require('fs'),
log = require('../util/log');

function getList(param, resolve) {
	if (param.regions.length == 0) {
		resolve([])
		return
	}
	param.page_num = param.page_num || 0;
	request({
		proxy : 'http://168.39.6.84:8080',
		url : 'http://api.map.baidu.com/place/v2/search?ak=hwPl2CSiaPPeSxru8GkZEiQf&output=json&query=%E4%B8%AD%E7%9F%B3%E5%8C%96%E3%80%81%E5%8A%A0%E6%B2%B9%E7%AB%99&page_size=20&page_num=' + param.page_num + '&scope=2&region=' + param.regions[0]
	}, (err, res, body) => {
		if (err) {
			log(err)
			return
		}
		var obj = JSON.parse(body),
		data = []
		obj.results.forEach(item => {
			data.push([item.name, item.address, item.location.lat, item.location.lng])
		})
		if (obj.total > (param.page_num + 1) * 20) {
			param.page_num++
		} else {
			param = {
				regions : param.regions.slice(1)
			}
		}
		getList(param, _data => {
			data.push.apply(data, _data)
			resolve(data)
		})
	})
}

getList({
	regions : [119, 137, 138, 139, 140, 141, 187, 197, 198, 199, 200, 201, 257, 258, 259, 301, 302, 303, 338, 339, 340]
}, data => {
	fs.writeFileSync('C:\\Users\\Administrator\\Desktop\\白名单.xlsx', xlsx.build([{
					name : '白名单',
					data : data
				}
			]), 'binary')
})
